package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.DatosportemplateDAO;
import py.com.alfa.escriba.persistence.dto.DatosportemplateDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.DatosportemplateService;

public class DatosportemplateServiceImpl implements DatosportemplateService{

  private DatosportemplateDAO datosportemplateDAO;

  public void setDatosportemplateDAO(DatosportemplateDAO datosportemplateDAO) {
    this.datosportemplateDAO = datosportemplateDAO;
  }

  @Override
  public List<DatosportemplateDTO> listDatosportemplate(InvocationContext ic) throws ServiceException {
    List<DatosportemplateDTO> datosportemplates =  datosportemplateDAO.listDatosportemplate();

    return datosportemplates;
  }

  @Override
  public DatosportemplateDTO getDatosportemplate(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    DatosportemplateDTO datosportemplate =  datosportemplateDAO.getDatosportemplateById(id);

    if (datosportemplate == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"datosportemplate.notfound", id);

    return datosportemplate;
  }

  @Override
  public void deleteDatosportemplate(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    DatosportemplateDTO datosportemplate =  datosportemplateDAO.getDatosportemplateById(id);

    if(datosportemplate == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"datosportemplate.notfound", id);

    try {
      datosportemplateDAO.deleteDatosportemplate(datosportemplate);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "datosportemplate.constraint.violation", id);
    }

  }

  @Override
  public DatosportemplateDTO updateDatosportemplate(Long id, DatosportemplateDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    DatosportemplateDTO datosportemplate = datosportemplateDAO.getDatosportemplateById(id);

    if (datosportemplate == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"datosportemplate.notfound", id);

    datosportemplate.setIdtemplate(dto.getIdtemplate());
    datosportemplate.setIddatocomplementario(dto.getIddatocomplementario());
    datosportemplate.setActivo(dto.getActivo());
 

    Utils.validarDTO(dto);

    datosportemplateDAO.updateDatosportemplate(dto);

    return datosportemplate;
  }

  @Override
  public DatosportemplateDTO createDatosportemplate(DatosportemplateDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      datosportemplateDAO.createDatosportemplate(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "datosportemplate.duplicate.key");
    }

    return dto;
  }

}